﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Tools;
using Win32;
using Model;

namespace 测试项目
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Process process1 = new Process();
        public string cs { get; set; }
        public const int KEYEVENTF_KEYUP = 2;
        private IntPtr ParentHandle { get; set; }
        /// <summary>
        /// 输入框句柄
        /// </summary>
        private IntPtr InputHandle { get; set; }
        /// <summary>
        /// Check按钮句柄
        /// </summary>
        private IntPtr CheckHandle { get; set; }
        private void Delay(int Millsecond)
        {
            DateTime current = DateTime.Now;
            while (current.AddMilliseconds(Millsecond) > DateTime.Now)
            {
                Application.DoEvents();
            }
            return;
        }
        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        private static extern int SetCursorPos(int x, int y);
        [DllImport("user32")]
        private extern static void mouse_event(int dwFlag, int dx, int dy, int dwData, IntPtr deExtraInfo);
        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        [DllImport("user32.dll")]//取设备场景 
        private static extern IntPtr GetDC(IntPtr hwnd);//返回设备场景句柄 
        [DllImport("gdi32.dll")]//取指定点颜色 
        private static extern int GetPixel(IntPtr hdc, Point p);
        public static void FormInsert(IntPtr hwnd, Panel panel)
        {
            User.SetParent(hwnd, panel.Handle);
            User.SetWindowLong(hwnd, User.GWL_STYLE, User.WS_VISIBLE);
            User.MoveWindow(hwnd, 0, 0, panel.Width, panel.Height, 1);
            User.ShowWindow(hwnd, User.SW_SHOWDEFAULT);
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            //System.Windows.Forms.Timer tim = new System.Windows.Forms.Timer();
            //tim.Interval = 1;
            //tim.Tick += delegate
            //{

            //};
            //tim.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.pictureBox1.Load(System.Environment.CurrentDirectory + "//截图.PNG");
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Process[] myproc = Process.GetProcesses();
            foreach (Process item in myproc)
            {
                if (item.ProcessName.Equals("Simba.data"))
                {
                    item.Kill();
                    Thread.Sleep(100);
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Point p = new Point(MousePosition.X, MousePosition.Y);//取置顶点坐标 
            IntPtr hdc = GetDC(new IntPtr(0));//取到设备场景(0就是全屏的设备场景) 
            int c = GetPixel(hdc, p);//取指定点颜色 
            int r = (c & 0xFF);//转换R 
            int g = (c & 0xFF00) / 256;//转换G 
            int b = (c & 0xFF0000) / 65536;//转换B 
            if (Color.FromArgb(r, g, b)==WriteModel.Fail)
            {

            }
            if (Color.FromArgb(r, g, b) == WriteModel.Pass)
            {

            }
            if (Color.FromArgb(r, g, b) == WriteModel.Write)
            {

            }
            button2.BackColor = Color.FromArgb(r, g, b);
        }
    }
}
